home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 2: Applications / Linux Cubed Series 2 - Applications.iso / utils / console / splitvt-.000 / splitvt- / splitvt-1.6.1 / parserc.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-01-05  |  6.1 KB  |  270 lines

  1.  
  2. /* Routines to parse the ~/.splitvtrc file and set default options */
  3.  
  4. #include <stdio.h>
  5. #include <ctype.h>
  6. #include "splitvt.h"
  7.  
  8. extern char *myputenv();  /* Portable setenv() function in misc.c */
  9.  
  10. #define WARN(X)    fprintf(stderr, X, lineno); sleep(2);
  11.  
  12. char *startupfile="%s/.splitvtrc";    /* %s is replaced by $HOME */
  13.  
  14.  
  15. char extract(arg)    /* get a char from x/^x format */
  16. char *arg;
  17. {
  18.     if ( *arg == '^' ) {
  19.         ++arg;
  20.         if ( islower(*arg) )
  21.             *arg=toupper(*arg);
  22.         return(*arg-'@');
  23.     }
  24.     return(*arg);
  25. }
  26.  
  27.  
  28. static int lineno=0;    /* The current line in the startup file */
  29.  
  30.  
  31. void set_something(args)
  32. char *args[];
  33. {
  34.     int i;
  35.  
  36.     if ( ! args[1] ) {
  37.         WARN("Warning: No argument to 'set' in ~/.splitvtrc line %d\n");
  38.         return;
  39.     }
  40.     if ( strcmp(args[1], "command_char") == 0 ) {
  41.         if ( ! args[2] ) {
  42.             WARN("Warning: No command_char to set in ~/.splitvtrc line %d\n");
  43.             return;
  44.         }
  45.         command_c=extract(args[2]);
  46.     } else if ( strcmp(args[1], "switch_char") == 0 ) {
  47.         if ( ! args[2] ) {
  48.             WARN("Warning: No switch_char to set in ~/.splitvtrc line %d\n");
  49.             return;
  50.         }
  51.         switch_c=extract(args[2]);
  52.     } else if ( strcmp(args[1], "quote_char") == 0 ) {
  53.         if ( ! args[2] ) {
  54.             WARN("Warning: No switch_char to set in ~/.splitvtrc line %d\n");
  55.             return;
  56.         }
  57.         quote_c=extract(args[2]);
  58.     } else if ( strcmp(args[1], "upper_lines") == 0 ) {
  59.         if ( ! args[2] ) {
  60.             WARN("Warning: No number of lines to set in ~/.splitvtrc line %d\n");
  61.             return;
  62.         }
  63.         if ( (i=atoi(args[2])) == 0 ) {
  64.             WARN("Warning: Invalid number of lines in ~/.splitvtrc line %d\n");
  65.             return;
  66.         } else
  67.             UU_lines=i;
  68.     } else if ( strcmp(args[1], "login") == 0 ) {
  69.         if ( ! args[2] ) {
  70.             WARN("Syntax error in ~/.splitvtrc line %d (Usage: set login [on|off])\n");
  71.         } else if ( strcmp(args[2], "on") == 0 )
  72.             dologin=1;
  73.         else if ( strcmp(args[2], "off") == 0 )
  74.             dologin=0;
  75.         else {
  76.             WARN("Syntax error in ~/.splitvtrc line %d (Usage: set login [on|off])\n");
  77.         }
  78.     } else {
  79.         WARN("Warning: Invalid parameter to 'set' in ~/.splitvtrc line %d\n");
  80.     }
  81. }
  82.  
  83. void set_argv(args)
  84. char *args[];
  85. {
  86.     int i;
  87.  
  88.     if ( ! args[1] ) {
  89.         WARN("Warning: Nothing to run in ~/.splitvtrc line %d\n");
  90.     }
  91.     if ( *args[1] == '-' ) {
  92.         if ( strcmp(&args[1][1], "upper") == 0 ) {
  93.             for ( i=2; args[i]; ++i ) {
  94.                 if ( (upper_args[i-2]=
  95.                 (char *)malloc(strlen(args[i])+1)) == NULL ) {
  96.                     perror("malloc");
  97.                     exit(5);
  98.                 }
  99.                 strcpy(upper_args[i-2], args[i]);
  100.             }
  101.             upper_args[i-2]=NULL;
  102.         } else if ( strcmp(&args[1][1], "lower") == 0 ) {
  103.             for ( i=2; args[i]; ++i ) {
  104.                 if ( (lower_args[i-2]=
  105.                 (char *)malloc(strlen(args[i])+1)) == NULL ) {
  106.                     perror("malloc");
  107.                     exit(5);
  108.                 }
  109.                 strcpy(lower_args[i-2], args[i]);
  110.             }
  111.             lower_args[i-2]=NULL;
  112.         } else {
  113.             WARN("Warning: Invalid argument to 'run' in ~/.splitvtrc line %d\n");
  114.         }
  115.         return;
  116.     }
  117.  
  118.     /* Straight command line for both upper and lower windows */
  119.     for ( i=1; args[i]; ++i ) {
  120.             if ( ((upper_args[i-1]=(char *)malloc(strlen(args[i])+1)) == NULL) ||
  121.                  ((lower_args[i-1]=(char *)malloc(strlen(args[i])+1)) == NULL) ) {
  122.                 perror("malloc");
  123.                 exit(5);
  124.             }
  125.             strcpy(upper_args[i-1], args[i]);
  126.             strcpy(lower_args[i-1], args[i]);
  127.     }
  128.     upper_args[i-1]=NULL;
  129.     lower_args[i-1]=NULL;
  130. }
  131.  
  132. void splitvtrc()
  133. {
  134.     char *home, splitvtrc[256];
  135.     FILE *rcfile;
  136.     char line[BUFSIZ], newline[BUFSIZ*2], *parsed[256];
  137.     char *head, *tail, *ptr, *subptr;
  138.     char *envptr, envbuf[128];
  139.     int  i, n, quoted=0;
  140.     int  ignoring=0, ifs=0;
  141.  
  142.     if ( (home=(char *)getenv("HOME")) == NULL )
  143.         home="";
  144.  
  145.     sprintf(splitvtrc, startupfile, home);
  146.     if ( (rcfile=fopen(splitvtrc, "r")) == NULL )
  147.         return;
  148.  
  149.     while ( fgets(line, BUFSIZ-1, rcfile) ) {
  150.         ++lineno;
  151.         /* Clean up leading and tailing whitespace */
  152.         for ( head=line; WHITESPACE(*head); ++head );
  153.         for ( tail=(head+strlen(head)); 
  154.             (WHITESPACE(*(tail-1)) && (tail > head)); --tail );
  155.         *tail='\0';
  156.         if ( (*head == '\0') || (*head == '#') )
  157.             continue;
  158.  
  159.         /* Parse the line */
  160.         tail=head; 
  161.         for ( i=0, head=ptr=newline; *tail; ) {
  162.             if ( WHITESPACE(*tail) ) {
  163.                 *(ptr++)='\0';
  164.                 parsed[i++]=head;
  165.                 do {
  166.                     ++tail;
  167.                 } while ( *tail && WHITESPACE(*tail) );
  168.                 head=ptr;
  169.             }
  170.             /* Not whitespace */
  171.  
  172.             /* Environment variable expansion */        
  173.             if ( *tail == '$' ) {
  174.                 n=0;
  175.                 for (subptr=(tail+1);isalpha(*subptr);++subptr)
  176.                     envbuf[n++]=(*subptr);
  177.                 envbuf[n]='\0';
  178.                 tail=subptr;
  179.                 if ( (envptr=(char *)getenv(envbuf)) ) {
  180.                     strcpy(ptr, envptr);
  181.                     ptr+=strlen(envptr);
  182.                 }
  183.                 continue;
  184.             }
  185.                 
  186.             /* Support backslash escape */
  187.             if ( *tail == '\\' )
  188.                 ++tail;
  189.  
  190.             if ( *tail )
  191.                 *(ptr++) = *(tail++);
  192.         }
  193.         *ptr='\0';  /* cap the new buffer with a null */
  194.         parsed[i++]=head;
  195.         parsed[i++]=NULL;
  196.  
  197.         /* Perform flow control (no nested if statements) */
  198.         if ( strcmp(parsed[0], "if") == 0 ) {
  199.             if ( !parsed[1] ) {
  200.                 WARN("Warning: bad if syntax in ~/.splitvtrc line %d\n");
  201.                 continue;
  202.             }
  203.             if ( ifs ) {
  204.                 WARN("Warning: ignoring nested if statement in ~/.splitvtrc line %d\n");
  205.                 continue;
  206.             } ++ifs;
  207.  
  208.             /* if <> syntax */
  209.             if ( ! parsed[2] ) {
  210.                 if ( *parsed[1] )
  211.                     ignoring=0;
  212.                 else
  213.                     ignoring=1;
  214.                 continue;
  215.             }
  216.  
  217.             /* if <> operand <> syntax */
  218.             if ( strcmp(parsed[2], "==") == 0 ) {
  219.                 if ( strcmp(parsed[1], parsed[3]) == 0 )
  220.                     ignoring=0;
  221.                 else
  222.                     ignoring=1;
  223.             } else if ( strcmp(parsed[2], "!=") == 0 ) {
  224.                 if ( strcmp(parsed[1], parsed[3]) == 0 )
  225.                     ignoring=1;
  226.                 else
  227.                     ignoring=0;
  228.             } else
  229.                 WARN("Warning: unknown if conditional in ~/.splitvtrc line %d\n");
  230.             continue;
  231.         }
  232.         if ( strcmp(parsed[0], "else") == 0 ) {
  233.             if ( ignoring )
  234.                 ignoring=0;
  235.             else
  236.                 ignoring=1;
  237.             continue;
  238.         }
  239.         if ( strcmp(parsed[0], "endif") == 0 ) {
  240.             --ifs;
  241.             ignoring=0;
  242.             continue;
  243.         }
  244.         if ( ignoring )
  245.             continue;
  246.  
  247.         /* Check for a variable setting */
  248.         if ( parsed[1] == NULL ) {
  249.             for ( n=0; parsed[0][n]; ++n ) {
  250.                 if ( parsed[0][n] == '=' )
  251.                     break;
  252.             }
  253.             if ( parsed[0][n] ) {
  254.                 myputenv(parsed[0]);
  255.                 continue;
  256.             }
  257.         }
  258.  
  259.         /* Check for various commands */
  260.         if ( strcmp(parsed[0], "set") == 0 )
  261.             set_something(parsed);
  262.         else if ( strcmp(parsed[0], "run") == 0 )
  263.             set_argv(parsed);
  264.         else {
  265.             WARN("Warning: Invalid directive in ~/.splitvtrc line %d\n");
  266.         }
  267.     }
  268.     return;
  269. }
  270.